﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    class MouseValidator : IValidator
    {
        public Dictionary<string, Tuple<User, int>> compare(User user, List<User> usersList)
        {
            Dictionary<string, Tuple<User, int>> comparison = new Dictionary<string, Tuple<User, int>>();
            return comparison;
        }

        public string validate(string keysDeserialized, string newKey)
        {
            Console.WriteLine("Klikniecia mysza: " + newKey);
            string[] splitedNewKey = newKey.Split(' ');

            string[] lines = Regex.Split(keysDeserialized, "\r\n");
            int bestMatchDiff = 9999999;
            string bestMatchLogin = "";

            Dictionary<string, int> numOfOccurences = new Dictionary<string, int>();
            numOfOccurences.Add("WM_LBUTTONDOWN", 0);
            numOfOccurences.Add("WM_RBUTTONDOWN", 0);
            numOfOccurences.Add("WM_MOUSEWHEEL", 0);
            numOfOccurences.Add("WM_LBUTTONDBLCLK", 0);
            numOfOccurences.Add("WM_MBUTTONDOWN", 0);

            // ile dane klikniecie wystapilo razy:
            foreach (string pattern in splitedNewKey)
            {
                if (pattern.Equals("WM_LBUTTONDOWN"))
                {
                    int num;
                    numOfOccurences.TryGetValue("WM_LBUTTONDOWN", out num);
                    num++;
                    numOfOccurences.Remove("WM_LBUTTONDOWN");
                    numOfOccurences.Add("WM_LBUTTONDOWN", num);
                }
                else if (pattern.Equals("WM_RBUTTONDOWN"))
                {
                    int num;
                    numOfOccurences.TryGetValue("WM_RBUTTONDOWN", out num);
                    num++;
                    numOfOccurences.Remove("WM_RBUTTONDOWN");
                    numOfOccurences.Add("WM_RBUTTONDOWN", num);
                }
                else if (pattern.Equals("WM_MOUSEWHEEL"))
                {
                    int num;
                    numOfOccurences.TryGetValue("WM_MOUSEWHEEL", out num);
                    num++;
                    numOfOccurences.Remove("WM_MOUSEWHEEL");
                    numOfOccurences.Add("WM_MOUSEWHEEL", num);
                }
                else if (pattern.Equals("WM_LBUTTONDBLCLK"))
                {
                    int num;
                    numOfOccurences.TryGetValue("WM_LBUTTONDBLCLK", out num);
                    num++;
                    numOfOccurences.Remove("WM_LBUTTONDBLCLK");
                    numOfOccurences.Add("WM_LBUTTONDBLCLK", num);
                }
                else if (pattern.Equals("WM_MBUTTONDOWN"))
                {
                    int num;
                    numOfOccurences.TryGetValue("WM_MBUTTONDOWN", out num);
                    num++;
                    numOfOccurences.Remove("WM_MBUTTONDOWN");
                    numOfOccurences.Add("WM_MBUTTONDOWN", num);
                }

            }

            Console.WriteLine("Czestotliwosc wystepowania:");
            foreach (string key in numOfOccurences.Keys)
            {
                int num; numOfOccurences.TryGetValue(key, out num);
                Console.WriteLine(key + " : " + num + ", ");
            }

            string login = "walidowanyUzytkownik";
            foreach (string line in lines)
            {
                if (line.Length == 0)   // if empty string then we don't count it
                    continue;

                // Console.WriteLine("Linia: " + line);
                string[] wordsInLine = line.Split(' ');
                string currentLogin = wordsInLine[0].Substring(0, wordsInLine[0].Length - 1);
                var hash = new HashSet<string>();   // hash storing unique Keyboard hits

                Dictionary<string, int> savedMouseOcc = new Dictionary<string, int>();
                savedMouseOcc.Add("WM_LBUTTONDOWN", 0);
                savedMouseOcc.Add("WM_RBUTTONDOWN", 0);
                savedMouseOcc.Add("WM_MOUSEWHEEL", 0);
                savedMouseOcc.Add("WM_LBUTTONDBLCLK", 0);
                savedMouseOcc.Add("WM_MBUTTONDOWN", 0);
                foreach (string word in wordsInLine)
                {
                    if (word.Equals("WM_LBUTTONDOWN"))
                    {
                        int num;
                        savedMouseOcc.TryGetValue("WM_LBUTTONDOWN", out num);
                        num++;
                        savedMouseOcc.Remove("WM_LBUTTONDOWN");
                        savedMouseOcc.Add("WM_LBUTTONDOWN", num);
                    }
                    else if (word.Equals("WM_RBUTTONDOWN"))
                    {
                        int num;
                        savedMouseOcc.TryGetValue("WM_RBUTTONDOWN", out num);
                        num++;
                        savedMouseOcc.Remove("WM_RBUTTONDOWN");
                        savedMouseOcc.Add("WM_RBUTTONDOWN", num);
                    }
                    else if (word.Equals("WM_MOUSEWHEEL"))
                    {
                        int num;
                        savedMouseOcc.TryGetValue("WM_MOUSEWHEEL", out num);
                        num++;
                        savedMouseOcc.Remove("WM_MOUSEWHEEL");
                        savedMouseOcc.Add("WM_MOUSEWHEEL", num);
                    }
                    else if (word.Equals("WM_LBUTTONDBLCLK"))
                    {
                        int num;
                        savedMouseOcc.TryGetValue("WM_LBUTTONDBLCLK", out num);
                        num++;
                        savedMouseOcc.Remove("WM_LBUTTONDBLCLK");
                        savedMouseOcc.Add("WM_LBUTTONDBLCLK", num);
                    }
                    else if (word.Equals("WM_MBUTTONDOWN"))
                    {
                        int num;
                        savedMouseOcc.TryGetValue("WM_MBUTTONDOWN", out num);
                        num++;
                        savedMouseOcc.Remove("WM_MBUTTONDOWN");
                        savedMouseOcc.Add("WM_MBUTTONDOWN", num);
                    }

                }

                int currentDiff = 0;

                // now compare
                Console.WriteLine("Klikniecia " + currentLogin + " : "  + line);
                foreach (string click in numOfOccurences.Keys)
                {
                        int num;
                        savedMouseOcc.TryGetValue(click, out num);
                        int num2;
                        numOfOccurences.TryGetValue(click, out num2);
                        currentDiff += Math.Abs(num - num2);
                        Console.WriteLine("Porownywane " + click + ": " + login + " - " + num2 + " , " + currentLogin + " : " + num + " , roznica ilosci klikniec: " + Math.Abs(num - num2) + ", roznica calkowita : " + currentDiff);
                }

                Console.WriteLine(login + "/" + currentLogin + " : calkowita roznica: " + currentDiff);
                if (currentDiff == bestMatchDiff)
                {
                    bestMatchDiff = currentDiff;
                    bestMatchLogin += (" "+ wordsInLine[0].Substring(0, wordsInLine[0].Length - 1));
                }
                else if (currentDiff < bestMatchDiff)
                {
                    bestMatchDiff = currentDiff;
                    bestMatchLogin = wordsInLine[0].Substring(0, wordsInLine[0].Length - 1);
                }

                Console.WriteLine("Najmniejsza roznica klikniec myszy: " + bestMatchDiff + ", uzytkownik: " + bestMatchLogin);
            }

            return bestMatchLogin;
        }
    }
}
